﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Controls
{
    public partial class PhanTrang : System.Web.UI.UserControl
    {
        public int numofpage;
        public int currentPage;
        public string catID;
        public int type;
        public int groupID;
        public string site;
        public string var;
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] listurl = Request.Url.AbsoluteUri.Split('/');
            string url = listurl[listurl.Length - 1];
            string tab = "";
            if (listurl.Length > 2)
                tab = listurl[listurl.Length - 2];

            if (url == "project")
            {
                site = "project";

                if (Request.QueryString["ID"] == null)
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_DuAn").Rows.Count;
                    int size = BDS3M.Controls.ListNewsDuAn.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListNewsDuAn.cur;
                    var = "";

                    catID = "";
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();

                }
                else
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_DuAn", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                    int size = BDS3M.Controls.ListNewsDuAn.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListNewsDuAn.cur;
                    var = "/category";
                    string name = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Category", "Category_ID1", Request.QueryString["ID"].ToString()).Rows[0]["Category_Name"].ToString();
                    catID = "/" + LIB.Converter.convertLink(name) + "-" + Request.QueryString["ID"].ToString();
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();

                }
            }



            if (url == "news")
            {
                site = "news";

                if (Request.QueryString["ID"] == null)
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_News").Rows.Count;
                    int size = BDS3M.Controls.ListContentNews.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentNews.cur;
                    var = "";

                    catID = "";
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();

                }
                else
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_News", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                    int size = BDS3M.Controls.ListContentNews.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentNews.cur;
                    var = "/category";
                    string name = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Category", "Category_ID1", Request.QueryString["ID"].ToString()).Rows[0]["Category_Name"].ToString();
                    catID = "/" + LIB.Converter.convertLink(name) + "-" + Request.QueryString["ID"].ToString();
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();

                }

            }



            if (url == "laws")
            {
                site = "laws";

                if (Request.QueryString["ID"] == null)
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_PhapLuat").Rows.Count;
                    int size = BDS3M.Controls.ListContentPhapLuat.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentPhapLuat.cur;
                    var = "";

                    catID = "";
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();

                }
                else
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_PhapLuat", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                    int size = BDS3M.Controls.ListContentPhapLuat.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentPhapLuat.cur;
                    var = "/category";
                    string name = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Category", "Category_ID1", Request.QueryString["ID"].ToString()).Rows[0]["Category_Name"].ToString();
                    catID = "/" + LIB.Converter.convertLink(name) + "-" + Request.QueryString["ID"].ToString();
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();

                }
            }



            if (url == "advisorys")
            {
                site = "advisorys";

                if (Request.QueryString["ID"] == null)
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_HoiDap").Rows.Count;
                    int size = BDS3M.Controls.ListContentHoiDap.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentHoiDap.cur;
                    var = "";

                    catID = "";
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();

                }
                else
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_HoiDap", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                    int size = BDS3M.Controls.ListContentHoiDap.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentHoiDap.cur;
                    var = "/category";
                    string name = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Category", "Category_ID1", Request.QueryString["ID"].ToString()).Rows[0]["Category_Name"].ToString();
                    catID = "/" + LIB.Converter.convertLink(name) + "-" + Request.QueryString["ID"].ToString();
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();
                }
            }

            if (url == "fengshui")
            {
                site = "fengshui";

                if (Request.QueryString["ID"] == null)
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_PhongThuy").Rows.Count;
                    int size = BDS3M.Controls.ListContentPhongThuy.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentPhongThuy.cur;
                    var = "";

                    catID = "";
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();
                }
                else
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_PhongThuy", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                    int size = BDS3M.Controls.ListContentPhongThuy.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentPhongThuy.cur;
                    var = "/category";
                    string name = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Category", "Category_ID1", Request.QueryString["ID"].ToString()).Rows[0]["Category_Name"].ToString();
                    catID = "/" + LIB.Converter.convertLink(name) + "-" + Request.QueryString["ID"].ToString();
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();
                }
            }


            if (url == "employment")
            {
                site = "employment";

                if (Request.QueryString["ID"] == null)
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_TuyenDung").Rows.Count;
                    int size = BDS3M.Controls.ListContentTuyenDung.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentTuyenDung.cur;
                    var = "";

                    catID = "";
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();
                }
                else
                {
                    int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_TuyenDung", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                    int size = BDS3M.Controls.ListContentTuyenDung.sizepage;
                    numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                    currentPage = BDS3M.Controls.ListContentTuyenDung.cur;
                    var = "/category";
                    string name = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Category", "Category_ID1", Request.QueryString["ID"].ToString()).Rows[0]["Category_Name"].ToString();
                    catID = "/" + LIB.Converter.convertLink(name) + "-" + Request.QueryString["ID"].ToString();
                    rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                    currentPage);
                    rptPaging.DataBind();
                }
            }


            if (url == "reals")
            {
                if (tab == "search")
                {
                    site = "reals";
                    if (Request.QueryString["IDLoai"] != null)
                    {
                        catID = Request.QueryString["IDLoai"];
                        if (catID == "1")
                        {
                            int n = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where " + TimKiemNangCao.condition , true).Rows.Count;
                            int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                            numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                            currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                            var = "/advance/" + LIB.Converter.convertLink(SanGiaoDich.name)+"/search";

                            catID = "-1";
                            rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                            currentPage);
                            rptPaging.DataBind();
                        }
                        else
                        {
                            TimKiemNangCao.catCondition = "AND LoaiTinRao_ID = " + catID;
                            int n = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where " + TimKiemNangCao.condition + TimKiemNangCao.catCondition, true).Rows.Count;

                            int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                            numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                            currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                            var = "/advance/" + LIB.Converter.convertLink(SanGiaoDich.name) + "/search";

                            catID = "-"+ Request.QueryString["IDLoai"];
                            rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                            currentPage);
                            rptPaging.DataBind();
                        }
                    }
                }
                else
                {
                    site = "reals";

                    if (Request.QueryString["content"] != null)
                    {
                        if (Request.QueryString["IDLoai"] != null)
                        {
                            catID = Request.QueryString["IDLoai"];
                            if (catID == "1")
                            {
                                int n = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where SanGiaoDich_TieuDe like '%" + Request.QueryString["ID"] + "%'", true).Rows.Count;
                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "/content/" + Request.QueryString["content"] + "/";
                                catID += "/";

                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                                currentPage);
                                rptPaging.DataBind();
                            }
                            else
                            {
                                int n = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where SanGiaoDich_TieuDe like '%" + Request.QueryString["content"] + "%' AND LoaiTinRao_ID = " + catID, true).Rows.Count;

                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "/content/" + Request.QueryString["content"] + "/";
                                catID += "/";
                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                                currentPage);
                                rptPaging.DataBind();
                            }
                        }
                        else
                        {
                            int n = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where SanGiaoDich_TieuDe like '%" + Request.QueryString["ID"] + "%'", true).Rows.Count;
                            int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                            numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                            currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                            var = "/content/" + Request.QueryString["content"];


                            rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                            currentPage);
                            rptPaging.DataBind();

                        }
                    }
                    else if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
                    {
                        if (Request.QueryString["IDLoai"] != null)
                        {
                            catID = Request.QueryString["IDLoai"];
                            if (catID == "1")
                            {
                                int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_SanGiaoDich", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "/category/" + LIB.Converter.convertLink(SanGiaoDich.name);

                                catID = "-" + Request.QueryString["ID"] + "-1";
                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                                currentPage);
                                rptPaging.DataBind();
                            }
                            else
                            {
                                int n = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where Category_ID1 = " + Request.QueryString["ID"] + "AND LoaiTinRao_ID = " + catID, true).Rows.Count;

                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "/category/" + LIB.Converter.convertLink(SanGiaoDich.name);

                                catID = "-" + Request.QueryString["ID"] + "-" + Request.QueryString["IDLoai"];
                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                                currentPage);
                                rptPaging.DataBind();
                            }
                        }

                    }
                    else
                    {
                        if (Request.QueryString["IDLoai"] != null)
                        {

                            catID = Request.QueryString["IDLoai"];
                            if (catID != "1")
                            {
                                int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_SanGiaoDich", "LoaiTinRao_ID", catID).Rows.Count;

                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "/category/san-giao-dich--";

                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                          currentPage);
                                rptPaging.DataBind();
                            }
                            else
                            {
                                int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_SanGiaoDich").Rows.Count;
                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "/category/san-giao-dich--";
                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                          currentPage);
                                rptPaging.DataBind();
                            }

                        }
                        else
                        {
                            if (tab == "search")
                            {
                                int n = BDS3M.Controls.TimKiemNangCao.all;
                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "";

                                catID = "";
                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                                currentPage);
                                rptPaging.DataBind();
                            }
                            else
                            {
                                int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_SanGiaoDich").Rows.Count;
                                int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                                numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                                currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                                var = "";

                                catID = "";
                                rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                                currentPage);
                                rptPaging.DataBind();
                            }

                        }

                    }
                }
            }



            if (url == "home")
            {
                site = "reals";
                if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
                {
                    if (Request.QueryString["IDLoai"] != null)
                    {
                        catID = Request.QueryString["IDLoai"];
                        if (catID == "1")
                        {
                            int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_SanGiaoDich", "Category_ID1", Request.QueryString["ID"].ToString()).Rows.Count;
                            int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                            numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                            currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                            var = "/category/" + LIB.Converter.convertLink(Index.name);

                            catID = "-" + Request.QueryString["ID"] + "-1";
                            rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                            currentPage);
                            rptPaging.DataBind();
                        }
                        else
                        {
                            int n = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where Category_ID1 = " + Request.QueryString["ID"] + "AND LoaiTinRao_ID = " + catID, true).Rows.Count;

                            int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                            numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                            currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                            var = "/category/" + LIB.Converter.convertLink(Index.name);

                            catID = "-" + Request.QueryString["ID"] + "-" + Request.QueryString["IDLoai"];
                            rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                            currentPage);
                            rptPaging.DataBind();
                        }
                    }

                }
                else
                {
                    if (Request.QueryString["IDLoai"] != null)
                    {

                        catID = Request.QueryString["IDLoai"];
                        if (catID != "1")
                        {
                            int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_SanGiaoDich", "LoaiTinRao_ID", catID).Rows.Count;

                            int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                            numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                            currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                            var = "/category/san-giao-dich--";

                            rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                      currentPage);
                            rptPaging.DataBind();
                        }
                        else
                        {
                            int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_SanGiaoDich").Rows.Count;
                            int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                            numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                            currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                            var = "/category/san-giao-dich--";
                            rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                      currentPage);
                            rptPaging.DataBind();
                        }

                    }
                    else
                    {
                        int n = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_SanGiaoDich").Rows.Count;
                        int size = BDS3M.Controls.ListContentSanGiaoDich.sizepage;
                        numofpage = (n % size == 0 ? (n / size) : (n / size) + 1);
                        currentPage = BDS3M.Controls.ListContentSanGiaoDich.cur;
                        var = "";

                        catID = "";
                        rptPaging.DataSource = BOL.ProductManager.Instance.GetTablePagingIndex(n, size,
                                                                        currentPage);
                        rptPaging.DataBind();

                    }

                }
            }
        }

    }
}
